package com.nbst.model;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.mail.internet.MimeMessage;

@Component
public class SendMail {
    @Autowired
    private JavaMailSender mailSender;
    private String sender="372091504@qq.com";
    private String receiver="";

    public void setReceiver(String dst){
        receiver=dst;
    }
    public void sendMail(String url)  {
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        try {

            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

            helper.setFrom(sender);
            helper.setTo(receiver);

            helper.setSubject("激活确认");

            String text = "请点击链接,完成激活确认:<a href='" + url + "'>" + url + "</a>";
            helper.setText(text, true);
        }catch (Exception e){
            System.out.println(e);
        }
        mailSender.send(mimeMessage);
    }
    public static SendMail m;
    @PostConstruct
    public void init() {
        m = this;
    }
    public static void send(String receiver,String url){
        m.setReceiver(receiver);
        m.sendMail(url);
    }
    public static void test(){
        m.sendMail("test");
    }

}
